// pages/task_forum/task_forum.js
const request = require('../../utils/promise.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userId: null,
    userInfo: null,
    taskId: null,
    taskName: '',
    contentArr: [
      // {userId: 1, username: '', avatarUrl: '', content: '', time: ''}
    ],
    scrollHeight: '100vh',
    toBottom: 0,
    toView: '',
    nputVal: '',
    contentStr: '',
    canIAddForum: false,
    isShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.setData({
      taskId: options.taskId,
      userId: wx.getStorageSync('userId'),
      canIAddForum: options.canIAddForum==1
    })
    
    this.reqForumMessageData();  // 请求加载讨论内容数据
    this.reqTaskInfo(); // 获取任务信息
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh() {
    this.reqForumMessageData();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面被用户分享时执行
   */
  onShareAppMessage() {
    return {
      title: '讨论'
    }
  },

  /**
   * 分享到朋友圈
   */
  onShareTimeline() {
    return {
      title: '讨论'
    }
  },

  /**
   * 请求加载数据
   */
  reqForumMessageData() {
    
    request.GET('/message/task/forums', {taskId: this.data.taskId, userId: this.data.userId}).then(res => {
      // console.log(res)
      if (res.data.resCode==200) {
        this.setData({
          contentArr: res.data.forumMessages,
        })
        this.setData({
          toView: 'msg-' + (this.data.contentArr.length - 1)
        })
      } else {
        wx.showToast({
          title: '获取数据异常',
          icon: 'error',
          duration: 1500
        })
      }
    })
  },

  /**
   * 获取任务信息
   */
  reqTaskInfo() {
    request.GET('/task/getTaskById', {taskId: this.data.taskId}).then(res => {
      if (res.statusCode==200) {
        this.setData({
          taskName: res.data.taskName
        })
      }
    })
  },

  /**
   * 点击弹窗显示添加讨论
   */
  clickAddForum() {
    this.setData({
      isShow: true
    })
  },

  /**
   * 隐藏 modal
   */
  clickHideModal() {
    this.setData({
      isShow: false,
      contentStr: ''
    })
  },

   /**
   * 评论输入值改变
   */
  bindInputChange(e) {
    this.setData({
      contentStr: e.detail.value
    })
  },

  /**
   * 点击保存
   */
  clickSaveTaskForum() {

    if (this.data.contentStr.trim().length==0) {
      wx.showToast({
        title: '内容不能为空',
        icon: 'none',
      })
      return;
    }

    wx.showToast({
      title: '保存中',
      icon: 'loading',
      duration: 30000,
      mask: true
    })
    
    const param = {userId: this.data.userId, taskId: this.data.taskId, contentStr: this.data.contentStr}
    request.GET('/task/forum/save', param).then(res => {
      //console.log(res)
      if (res.data.resCode==200) {
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 1000,
          mask: true
        })

        this.setData({
          isShow: false,
          contentStr: ''
        })

        this.reqForumMessageData();
      } else {
        wx.showToast({
          title: '保存异常',
          icon: 'error',
          mask: true
        })
      }
    })
  },

})
